package com.atguigu.p2;

/*
 * CustomerView为主模块，负责菜单的显示和处理用户操作
 * 
 */
public class CustomerView {
	//属性
	private CustomerList customerlist = new CustomerList(10);
	
	//构造器里初始化一个客户
//	public CustomerView()
//	{
//		Customer customer = new Customer("普元庆",'女',18,"18214376259","2073958890@qq.com");
//		customerlist.addCustomer(customer);
//	}
	//方法
	/**
	 * 显示《客户信息管理软件》界面的方法
	 */
	public void enterMainMenu()
	{
		boolean flag = true;
		while(flag)
		{
			System.out.println("----------------客户信息管理软件----------------");
			System.out.println("                   1 添加客户");
			System.out.println("                   2 修改客户");
			System.out.println("                   3 删除客户");
			System.out.println("                   4 客户列表");
			System.out.println("                   5 退出");
			System.out.print("     请选择(1-5):");
			char menu = CMUtility.readMenuSelection();
			switch(menu)
			{
				case '1':
				{
					addNewCustomer();
					break;
				}
				case '2':
				{
					modifyCustomer();
					break;
				}
				case '3':
				{
					deleteCustomer();
					break;
				}
				case '4':
				{
					listAllCustomers();
					break;
				}
				case '5':
				{
					System.out.println();
					System.out.print("确认是否退出(Y/N):");
					char isexit = CMUtility.readConfirmSelection();
					if(isexit == 'Y')
					{
						flag=false;
					}
				}
			}
			
		}
		
	}
	
	/**
	 * 添加客户的操作
	 */
	public void addNewCustomer()
	{
		//System.out.println("添加客户的操作");
		System.out.println("----------------------------添加客户----------------------");
		System.out.print("姓名：");
		String name = CMUtility.readString(10);
		System.out.print("性别：");
		char gender = CMUtility.readChar();
		System.out.print("年龄：");
		int age = CMUtility.readInt();
		System.out.print("电话号码：");
		String phone = CMUtility.readString(12);
		System.out.print("电子邮箱：");
		String email = CMUtility.readString(30);
		
		//创建对象来封装上面的信息
		Customer cust = new Customer(name,gender,age,phone,email);
		
		boolean isSuccess = customerlist.addCustomer(cust);
		if(isSuccess)
		{
			System.out.println("-----------------------------添加完成--------------------------");
			System.out.println();
		}
		else
		{
			System.out.println("-----------------------------添加失败-----------------------------");
		}
	}
	
	/**
	 * 修改客户的操作
	 */
	public void modifyCustomer()
	{
		//System.out.println("修改客户的操作");
		System.out.println("----------------------------修改客户-----------------------");
		int number;
		Customer cust;
		for(;;)
		{
			System.out.print("请选择待修改客户编号(-1退出)：");
			number = CMUtility.readInt();
			if(number == -1)
			{
				return;
			}
			cust =customerlist.getCustomer(number-1);//调用获取指定用户信息的方法来判断
			                                                 //是否有待修改客户的信息
			if(cust == null)
			{
				System.out.println("无法找到指定用户！");
			}
			else
			{//说明已找到待修改客户
				break;
			}
		}
		//修改客户信息
		System.out.print("姓名("+cust.getName()+")：");
		String name = CMUtility.readString(10, cust.getName());
		System.out.print("性别("+cust.getGender()+")：");
		char gender = CMUtility.readChar(cust.getGender());
		System.out.print("年龄("+cust.getAge()+")：");
		int age = CMUtility.readInt(cust.getAge());
		System.out.print("电话("+cust.getPhone()+")：");
		String phone = CMUtility.readString(12, cust.getPhone());
		System.out.print("邮箱("+cust.getEmail()+")：");
		String email = CMUtility.readString(30, cust.getEmail());
		
		//创建来封装上面的修改信息
		Customer customer = new Customer(name,gender,age,phone,email);
		boolean isRpalced = customerlist.replaceCustomer(number-1, customer);
		if(isRpalced)
		{
			System.out.println("---------------------------------修改完成----------------------------");
			System.out.println();
		}
		else
		{
			System.out.println("---------------------------------修改失败-------------------------------");
		}
	}
	
	/**
	 * 删除客户的操作
	 */
	public void deleteCustomer()
	{
		//System.out.println("删除客户的操作");
		System.out.print("请选择待删除客户编号(-1退出)：");
		int number;
		for(;;)
		{
			number = CMUtility.readInt();
			if(number == -1)
			{
				return;
			}
			Customer customer = customerlist.getCustomer(number-1);//调用获取指定用户信息的方法来判断
			if(customer == null)                                   //是否有待删除客户的信息
			{
				System.out.println("无法找到指定用户！");
			}
			else
			{
				break;
			}
		}
		//找到了指定的用户
		System.out.println();
		System.out.print("确认是否删除(Y/N):");
		char isDelete = CMUtility.readChar();
		if(isDelete == 'Y')
		{
			boolean deleteSuccess = customerlist.deleteCustomer(number-1);
			if(deleteSuccess)
			{		
				System.out.println("--------------------------删除成功---------------------------");
				System.out.println();
			}
			else
			{
				System.out.println("--------------------------删除失败----------------------------");
			}
		}
	}
	
	/**
	 * 显示客户列表的操作
	 */
	public void listAllCustomers()
	{
		//System.out.println("显示客户列表的操作");
		System.out.println("----------------------------客户列表------------------------------");
		System.out.println();
		if(customerlist.getTotal() == 0)
		{
			System.out.println("没有记录！");
		}
		else
		{
			System.out.println("编号\t姓名\t性别\t年龄\t电话\t\t电话邮箱");
			Customer custs[] = customerlist.getAllCustomers();
			for(int i=0;i<custs.length ;i++)
			{
				Customer cust =custs[i];//将数组里的客户赋给一个Customer 类的变量便于它调用Customer类的get方法。
				System.out.println((i+1)+cust.toString());
				System.out.println();
			}
		}
		System.out.println("-----------------------------客户列表完成---------------------------");
		System.out.println();
	}
}

